<?php

use Webman\Route;
use support\Request;
use Intervention\Image\ImageManagerStatic as Image;

Route::any('/app/laoha/api/{controller}[/[{action}]]', function($request, $controller, $action='index'){
    $class_name = 'plugin\\laoha\\app\\controller\\' . $controller.'Controller';
    $controller = new $class_name;
    $request->controller = $class_name;
    $request->action = $action;
    $request->api = true;
    return call_user_func([$controller, $action], $request);
});

Route::any('/thumb/{w:\d+}x{h:\d+}/app/{app}/[{path:.+}]', function(Request $request,$w=200,$h=200,$app='',$path=''){
    if(strpos($path,'..')!==false || $path==''){
        return '';
    }
    $real_path = base_path()."/plugin/$app/public";
    $real_file="$real_path/$path";
    $thumb_file = $request->path();  //"/thumb/$w"."x$h/app/$app/$path";
    $real_thumb_file=public_path().$thumb_file;
    
    //return $real_thumb_file;
    if(is_file($real_thumb_file)){
        return response()->withFile($real_thumb_file);
    }elseif(is_file($real_file)){
        $arr = str2arr($path,'/');
        $file_name = end($arr);
        $real_thumb_path = str_replace('/' . $file_name, '', $real_thumb_file);        
        if (!is_dir($real_thumb_path)) {
            mkdir($real_thumb_path, 0777, true);
        }
        $img = Image::make($real_file);
        if ($w == 0) {
            $img->resize(null, $h, function ($constraint) {
                $constraint->aspectRatio();
            });
        } elseif ($h == 0) {
            $img->resize($w, null, function ($constraint) {
                $constraint->aspectRatio();
            });
        } else {
            $w = $w>1000?1000:$w;
            $h = $h>1000?1000:$h;
            $img->fit($w, $h);
        }     
        $img->save($real_thumb_file);   
        return response()->withFile($real_thumb_file);
    }else{
        return '';
    }
});

Route::any('/thumb/{w:\d+}x{h:\d+}/[{path:.+}]', function(Request $request,$w=200,$h=200,$path=''){
    
    if(strpos($path,'..')!==false || $path==''){
        return '';
    }
    $real_path = public_path();
    $real_file="$real_path/$path";
    $thumb_file = $request->path();       //"/thumb/$w"."x$h/$path";    
    $real_thumb_file=public_path().$thumb_file;
    
    if(is_file($real_thumb_file)){  
        return response()->withFile($thumb_file);
    }elseif(is_file($real_file)){
        $arr = str2arr($path,'/');
        $file_name = end($arr);
        $real_thumb_path = str_replace('/' . $file_name, '', $real_thumb_file);
        if (!is_dir($real_thumb_path)) {
            mkdir($real_thumb_path, 0777, true);
        }
        
        $img = Image::make($real_file);
        if ($w == 0) {
            $img->resize(null, $h, function ($constraint) {
                $constraint->aspectRatio();
            });
        } elseif ($h == 0) {
            $img->resize($w, null, function ($constraint) {
                $constraint->aspectRatio();
            });
        } else {            
            $w = $w>1000?1000:$w;
            $h = $h>1000?1000:$h;
            $img->fit($w, $h);
        }        
        $img->save($real_thumb_file);
        return response()->withFile($real_thumb_file);
    }else{
        return '';
    }
});